package com.fgs.common.widget.itemView;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.other.RegionData;
import com.fgs.common.utils.ToastUtil;
import com.xuexiang.xui.widget.picker.widget.OptionsPickerView;
import com.xuexiang.xui.widget.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnOptionsSelectListener;
import com.xuexiang.xutil.system.KeyboardUtils;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class RegionItemView extends LinearLayout {
    private LinearLayout mItemLinearLayout;
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private TextView mContentTextView;
    private ImageView mArrowImageView;
    private String mTitle;
    private String mContent;
    private String mContentHint;
    private int mTitleTextColor;
    private int mContentTextColor;
    private int mContextHintColor;
    private boolean isShowArrow;
    private boolean isShowTittle;
    private boolean isShowTipText;
    private boolean isItemClickable;

    private boolean mHasLoaded;
    private OptionsPickerView mProvincePickerView;

    private OnRegionSelectedCallback onRegionCallback;

    public RegionItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public RegionItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        mHasLoaded = RegionData.getInstance().loadProvinceData(RegionData.TYPE_NORMAL);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RegionItemView);
        int contentTextGravity;
        try {
            mTitle = typedArray.getString(R.styleable.RegionItemView_RegionItemTitle);
            mContent = typedArray.getString(R.styleable.RegionItemView_RegionItemContent);
            mContentHint = typedArray.getString(R.styleable.RegionItemView_RegionItemContentHint);
            mTitleTextColor = typedArray.getColor(R.styleable.RegionItemView_RegionItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            mContentTextColor = typedArray.getColor(R.styleable.RegionItemView_RegionItemContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
            mContextHintColor = typedArray.getColor(R.styleable.RegionItemView_RegionItemContentHintTextColor, context.getResources().getColor(R.color.colorTextGray));

            isShowArrow = typedArray.getBoolean(R.styleable.RegionItemView_RegionItemIsShowArrow, true);
            isShowTittle = typedArray.getBoolean(R.styleable.RegionItemView_RegionItemIsTittleShow, true);
            isShowTipText = typedArray.getBoolean(R.styleable.RegionItemView_RegionItemIsShowTip, false);
            isItemClickable = typedArray.getBoolean(R.styleable.RegionItemView_RegionItemIsClickable, true);
            contentTextGravity = typedArray.getInt(R.styleable.RegionItemView_RegionItemContentGravity, 1);
        } finally {

            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_region_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mItemLinearLayout = view.findViewById(R.id.layout_region_item);
        mTipTextView = view.findViewById(R.id.layout_region_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_region_item_titleTextView);
        mContentTextView = view.findViewById(R.id.layout_region_item_contentTextView);
        mArrowImageView = view.findViewById(R.id.layout_region_item_rightArrowImageView);

        setTitle(mTitle);
        setContent(mContent);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(mContentTextColor);
        setContextHintColor(mContextHintColor);
        setShowTitle(isShowTittle);
        setShowArrow(isShowArrow);
        setShowTip(isShowTipText);
        setItemClickable(isItemClickable);

        setContentTextGravity(contentTextGravity);

        mItemLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemClickable) {
                    selectRegion();
                }
            }
        });

    }

    public void selectRegion() {
        hideKeyboard();
        if (!mHasLoaded) {
            ToastUtil.showShortToast("数据加载中...");
            return;
        }
        if (mProvincePickerView == null) {

            mProvincePickerView = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
                @Override
                public boolean onOptionsSelect(View v, int options1, int options2, int options3) {
                    //返回的分别是三个级别的选中位置
                    String province = RegionData.getInstance().getProvinceInfo(options1).name;
                    String city = RegionData.getInstance().getCityInfo(options1, options2).name;
                    String region = RegionData.getInstance().getAreaInfo(options1, options2, options3).name;

                    String regionCode = RegionData.getInstance().getAreaInfo(options1, options2, options3).code;

                    String tx = province + " " + city + " " + region;
                    setContent(tx);

                    RegionInfo regionInfo = new RegionInfo();
                    regionInfo.province = province;
                    regionInfo.city = city;
                    regionInfo.county = region;
                    regionInfo.code = regionCode;
                    if (onRegionCallback != null) {
                        onRegionCallback.regionCallback(regionInfo);
                    }

                    return false;
                }
            })

                    .setTitleText("地址选择")
                    .setDividerColor(Color.BLACK)
                    //切换选项时，还原到第一项
                    .isRestoreItem(true)
                    //设置选中项文字颜色
                    .setTextColorCenter(Color.BLACK)
                    .setContentTextSize(20)
                    .isDialog(false)
                    .setSelectOptions(RegionData.getInstance().getDefaultProvince(), RegionData.getInstance().getDefaultCity(), RegionData.getInstance().getDefaultArea())
                    .build();

            mProvincePickerView.setPicker(RegionData.getInstance().getProvinceItems(), RegionData.getInstance().getCityItems(), RegionData.getInstance().getAreaItems());//三级选择器
            mProvincePickerView.show();
        }
        mProvincePickerView.show();
    }

    protected void hideKeyboard() {

        try {
            KeyboardUtils.hideSoftInput((Activity) getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void setShowTitle(boolean isShowTittle) {
        if (isShowTittle) {
            mTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mTitleTextView.setVisibility(View.GONE);
        }
    }

    private void setContentTextGravity(int gravity) {
        switch (gravity) {
            case 0:
                mContentTextView.setGravity(Gravity.LEFT);
                break;
            case 1:
                mContentTextView.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mContentTextView.setGravity(Gravity.CENTER);
                break;
        }
    }

    public void setItemClickable(boolean isClickable) {
        this.isItemClickable = isClickable;
        setClickable(isClickable);
        setEnabled(isClickable);
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public String getContent() {
        return mContent;
    }

    public void setContent(String content) {
        this.mContent = content;
        mContentTextView.setText(content);
    }


    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        if (!TextUtils.isEmpty(contentHint)) {

            mContentTextView.setHint(contentHint);
        }
    }


    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    public void setContentTextColor(int contentTextColor) {
        this.mContentTextColor = contentTextColor;
        mContentTextView.setTextColor(contentTextColor);
    }


    public void setContextHintColor(int contextHintColor) {
        this.mContextHintColor = contextHintColor;
        mContentTextView.setHintTextColor(contextHintColor);
    }

    public void setShowArrow(boolean showArrow) {
        isShowArrow = showArrow;
        if (showArrow) {
            mArrowImageView.setVisibility(View.VISIBLE);
        } else {
            mArrowImageView.setVisibility(View.GONE);
        }
    }


    private void setShowTip(boolean isShowTipText) {

        this.isShowTipText = isShowTipText;
        if (isShowTipText) {
            mTipTextView.setVisibility(View.VISIBLE);
        } else {
            mTipTextView.setVisibility(View.INVISIBLE);
        }
    }

    public interface OnRegionSelectedCallback {
        void regionCallback(RegionInfo info);
    }

    public void setOnRegionCallback(OnRegionSelectedCallback onRegionCallback) {
        this.onRegionCallback = onRegionCallback;
    }
}
